
//  DataManager.m
//  WeGo
//
//  Created by sandMan on 16/1/20.
//  Copyright © 2016年 sandMan. All rights reserved.
//

#import "DataManager.h"

//首先导入头文件信息
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <net/if.h>

#define IOS_CELLULAR    @"pdp_ip0"
#define IOS_WIFI        @"en0"
#define IP_ADDR_IPv4    @"ipv4"
#define IP_ADDR_IPv6    @"ipv6"

#define ENV             @"test"
#define VERSION         @"v1"

@interface DataManager()

@end

@implementation DataManager

+ (instancetype)manager {
    
    static DataManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DataManager alloc] init];
    });
    return manager;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        NSString * baseUrl;
        
        baseUrl = [NSString stringWithFormat:ZYHostApi];
        
        self.httpmanager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];
         AFHTTPRequestSerializer *requestSeializer = [AFHTTPRequestSerializer serializer];
         self.httpmanager.requestSerializer = requestSeializer;
        // AFJSONRequestSerializer *requestSeializer = [AFJSONRequestSerializer serializer];
        // self.httpmanager.requestSerializer = requestSeializer;

        [requestSeializer setTimeoutInterval:10];//等待服务器处理时间
    }
    return self;
}

- (NSURLSessionDataTask *)postRequestWithUrl:(NSString *)url param:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    [self.httpmanager.requestSerializer setValue:[NSString generateTradeNO] forHTTPHeaderField:@"X-Ca-Nonce"];
    [self.httpmanager.requestSerializer setValue:ZYUserInfoModel.sharedInfoModel.userModel.token forHTTPHeaderField:@"token"];
    
    
    NSLog(@"url --> %@", url);
    NSLog(@"params --> %@", param);
    
    return [self.httpmanager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"result --> %@", responseObject);
        XRCommonModel *model = [XRCommonModel commonModelWithKeyValues:responseObject dataClass:nil];
        if (block) {
            block(model, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error --> %@", error);
        if (block) {
            XRCommonModel *model = [XRCommonModel new];
            model.error = @"网络错误,请稍后重试";
            block(model,error);
        }
    }];
}

#pragma mark - 登录
- (NSURLSessionDataTask *)postLoginParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    [self.httpmanager.requestSerializer setValue:@"" forHTTPHeaderField:@"Connection"];
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/gasLogin", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 获取验证码
- (NSURLSessionDataTask *)postPhoneCodeParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/SMS/%@/phoneCode", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 更新用户信息
- (NSURLSessionDataTask *)postGetTimKeyParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/getTimKey", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 添加店员
- (NSURLSessionDataTask *)postAddPersonParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/addUser", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 查询店员
- (NSURLSessionDataTask *)postFindPersonParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/getUserByManager", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 修改员工信息
- (NSURLSessionDataTask *)postUpdatePersonParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/updateUser", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 删除店员
- (NSURLSessionDataTask *)postDeleteUserParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/deleteUser", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 查询油品
- (NSURLSessionDataTask *)postFindOilParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/oil/%@/findOil", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 添加油品
- (NSURLSessionDataTask *)postAddOilParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/oil/%@/addOil", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 修改油品
- (NSURLSessionDataTask *)postUpdateOilParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/oil/%@/updateOil", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 删除油品
- (NSURLSessionDataTask *)postDeleteOilParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/oil/%@/deleteOil", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 查看公司信息
- (NSURLSessionDataTask *)postFindCompanyInfoParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    NSString *url = [NSString stringWithFormat:@"/%@/gasCompany/%@/findCompanyInfo", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 获取账单人员
- (NSURLSessionDataTask *)postGetBillUserParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasBill/%@/getBillUser", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 添加公司图片
- (NSURLSessionDataTask *)postAddCompanyPictureParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasCompany/%@/addCompanyPicture", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}


#pragma mark - 根据userId查询账单
- (NSURLSessionDataTask *)postGetBillInfoParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
 
    NSString *url = [NSString stringWithFormat:@"/%@/gasBill/%@/getBillInfo", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 获取账单最早日期
- (NSURLSessionDataTask *)postGetCompanyDateParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasCompany/%@/getCompanyDate", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 油卡消费
- (NSURLSessionDataTask *)postOilCardConsumeParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasOilCard/%@/oilCardConsume", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 经营报表
- (NSURLSessionDataTask *)postStatementParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasBill/%@/statement", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 查询今日最新账单
- (NSURLSessionDataTask *)postTodayBillInfoParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasBill/%@/todayBillInfo", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 修改手机号
- (NSURLSessionDataTask *)postUpdatePhoneParam:(NSDictionary *)param block:(void (^)(XRCommonModel *model,NSError *error))block {
    
    NSString *url = [NSString stringWithFormat:@"/%@/gasUser/%@/updatePhone", ENV, VERSION];
    return [self postRequestWithUrl:url param:param block:block];
}

#pragma mark - 获取oss权限
- (NSURLSessionDataTask *)getUserOssAuthorityWithBlock:(void (^)(NSDictionary *posts,En_ResponseStatus *status,NSError *error))block {
    
    [self.httpmanager.requestSerializer setValue:[NSString generateTradeNO] forHTTPHeaderField:@"X-Ca-Nonce"];
    
    NSString *url = [NSString stringWithFormat:@"permissions/v1/oss"];

    return [self.httpmanager POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *arrayFromResponse = [responseObject valueForKey:@"data"];
        En_ResponseStatus *status = [[En_ResponseStatus alloc]init];
        status.code = [responseObject valueForKey:@"success"];
        status.msg = [responseObject valueForKey:@"error"];
        if (block) {
            block(arrayFromResponse,status,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (block) {
            block([NSDictionary dictionary],nil,error);
        }
    }];
}

@end
